package org.cuckoos.android;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import org.cuckoos.android.command.AbstractCommand;
import org.cuckoos.android.command.AnsentryCommand;
import org.cuckoos.android.command.EntryCommand;
import org.cuckoos.android.command.ReciveMsgCommand;
import org.cuckoos.android.command.SendMsgCommand;
import org.cuckoos.android.domain.Command;
import org.cuckoos.android.domain.Constant;
import org.cuckoos.android.ui.AppActivity;

import android.app.Activity;
import android.util.Log;

public class DefaultSocketService extends SocketService implements Runnable {

	private boolean isStarted = false;
	private List<AbstractCommand> handlers = new ArrayList<AbstractCommand>();
	Activity activity;
	DatagramSocket ds = null;
	
	/**
	 * 
	 */
	public void start(Activity activity) {
		this.activity = activity;
		isStarted = true;
		initDs();
		initHandler(activity);
		new Thread(this).start();
	}
	
	@Override
	public void stop() {
		isStarted = false;
		handlers.clear();
		ds.close();
		ds = null;
		
	}

	private void initDs() {
		try {
			ds = new DatagramSocket(Constant.DEFAULT_PORT);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initHandler(Activity activity) {
		handlers.add(new EntryCommand(ds,(AppActivity) activity));
		handlers.add(new AnsentryCommand(ds,(AppActivity) activity));
		handlers.add(new SendMsgCommand(ds,(AppActivity) activity));
		handlers.add(new ReciveMsgCommand(ds));
	}


	@Override
	public void run() {
		startService();
	}

	private void startService() {
		try {
			byte[] buf = new byte[Constant.DEFAULT_BUFFERE_SIZE];
			DatagramPacket packet = new DatagramPacket(buf,
					Constant.DEFAULT_BUFFERE_SIZE);
			while (isStarted) {
				buf = new byte[Constant.DEFAULT_BUFFERE_SIZE];
				ds.receive(packet);
				buf = packet.getData();
				String receiveCommand = new String(buf,Constant.DEFAULT_CHARSET);
				Command command = new Command();
				command.fromString(receiveCommand);
				command.getUser().setIp(packet.getAddress().getHostAddress());   //设置应答ip
				try{
				for (AbstractCommand handler : handlers) {
					if (handler.isHandle(command)) {
						handler.handle(command);
					}
				}
				}catch(Exception e){
					e.printStackTrace();
				}

			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(ds != null){
				ds.close();//TODO - try re create socket
			}
			Log.e(this.getClass().getSimpleName(), "");
		}
	}
		
	
	public DatagramSocket getSocket(){
		return ds;
	}

	@Override
	public void addHandler(AbstractCommand handler) {
		handlers.add(handler);
	}

	@Override
	public void removeHandler(AbstractCommand handler) {
		handlers.remove(handler);
	}

	

}
